﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.Extensions;

namespace Ue.Infrastructure.UI.SkinnedControls
{
    /// <summary>
    /// 数据显示控件，皮服文件可以指定，数据控件名必须是gridView
    /// </summary>
    public abstract class SkinnedGridView : SkinnedControl
    {
        #region 字段、委托

        private Control gridViewControl;
        private IAspNetPager aspNetPager;
        public event EventHandler GridViewDataBindHandler;
        public event Func<object, string> BuildDetailUrlHandler;
        public event Func<object, string> BuildDeleteUrlHandler;
        public event Func<object, string> BuildEditUrlHandler;
        public event Func<object, string, object[], string> BuildActionUrlHandler;

        public SkinnedGridView()
        {
            this.aspNetPager = this.BuildAspNetPager();
            this.PageSize = 30;
            this.EnableAspNetPager = true;
            this.TitleMaxLength = 140;
        }

        #endregion

        protected abstract void GridViewDataBind(object sender, EventArgs args);
        protected abstract IAspNetPager BuildAspNetPager();

        /// <summary>
        /// 是否启用分页控件
        /// </summary>
        public bool EnableAspNetPager { get; set; }

        /// <summary>
        /// 总记录条数
        /// </summary>
        public int RecordCount
        {
            get { return this.aspNetPager.RecordCount; }
            set { this.aspNetPager.RecordCount = value; }
        }

        public int PageIndex
        {
            get { return Page.Request.GetQueryValue<int>(this.aspNetPager.UrlPageIndexName, 1); }
        }

        public int PageSize
        {
            get { return this.aspNetPager.PageSize; }
            set { this.aspNetPager.PageSize = value; }
        }

        /// <summary>
        /// 分页控件地址重写规则
        /// </summary>
        public string AspNetPagerUrlRewritePattern
        {
            get { return this.aspNetPager.UrlRewritePattern; }
            set { this.aspNetPager.UrlRewritePattern = value; }
        }

        /// <summary>
        /// 标题最大长度
        /// </summary>
        public int TitleMaxLength { get; set; }

        #region public methods

        public int GetItemIndex(IDataItemContainer container)
        {
            return (this.PageIndex - 1) * this.PageSize + container.DataItemIndex + 1;
        }

        public virtual string BuildDetailUrl(object sender)
        {
            return this.BuildDetailUrl(string.Empty, sender);
        }
        public virtual string BuildDetailUrl(string defaultUrl,object sender)
        {
            return this.BuildUrl(
                this.BuildDetailUrlHandler,
                sender,
                () => this.BuildDetailUrlHandler(sender),
                defaultUrl);
        }

        public virtual string BuildEditUrl(object sender)
        {
            return this.BuildEditUrl(string.Empty,sender);
        }
        public virtual string BuildEditUrl(string defaultUrl, object sender)
        {
            return this.BuildUrl(
                this.BuildEditUrlHandler,
                sender,
                () => this.BuildEditUrlHandler(sender),
                defaultUrl);
        }

        public virtual string BuildDeleteUrl(object sender)
        {
            return this.BuildDeleteUrl(string.Empty,sender);
        }
        public virtual string BuildDeleteUrl(string defaultUrl, object sender)
        {
            return this.BuildUrl(
                this.BuildDeleteUrlHandler,
                sender,
                () => this.BuildDeleteUrlHandler(sender),
                defaultUrl);
        }

        public virtual string BuildActionUrl(object sender, string actionName, params object[] args)
        {
            return this.BuildActionUrl(string.Empty,sender,actionName,args);
        }
        public virtual string BuildActionUrl(string defaultUrl,object sender,string actionName,params object[] args)
        {
            return this.BuildUrl(
                this.BuildActionUrlHandler,
                sender,
                ()=>this.BuildActionUrlHandler(sender,actionName,args),
                defaultUrl);
        }

        protected string BuildUrl(object handler,object sender,Func<string> acquire)
        {
            return this.BuildUrl(handler, sender, acquire, string.Empty);
        }
        protected string BuildUrl(object handler, object sender, Func<string> acquire,string defaultUrl)
        {
            if (string.IsNullOrEmpty(defaultUrl))
            {
                defaultUrl = "javascript:void(0);";
            }

            return handler != null && sender != null
                ? acquire()
                : defaultUrl;
        }

        #endregion

        #region 私有方法

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            gridViewControl = skin.FindControl("gridView");
            if (gridViewControl == null)
            {
                throw new ArgumentNullException("皮肤文件中没有Id为gridView的数据控件。");
            }

            this.BindDataViewControl();            

            if (this.EnableAspNetPager)
            {
                //因为AspNetPager的一些配置信息需要通过控件的属性从页面上传过来，所以不能在构造方法配置AspnetPager,而是在加入控件树之前配置
                //this.DeployAspNetPager(this.aspNetPager);
                if (this.aspNetPager.PageCount > 1 && this.aspNetPager is Control)
                {
                    gridViewControl.Controls.Add(this.aspNetPager as Control);
                }

            }
        }

        /// <summary>
        /// 绑定数据控件控件
        /// </summary>
        private void BindDataViewControl()
        {
            if (this.GridViewDataBindHandler == null)
            {
                this.GridViewDataBindHandler += new EventHandler(GridViewDataBind);
            }

            this.GridViewDataBindHandler(this.gridViewControl, EventArgs.Empty);
        }

        /// <summary>
        /// 配置AspNetPager
        /// </summary>
        //protected virtual void DeployAspNetPager(AspNetPager aspNetPager)
        //{
        //    aspNetPager.UrlPaging = true;
        //    aspNetPager.HorizontalAlign = HorizontalAlign.Center;

        //    aspNetPager.FirstPageText = "首页";
        //    aspNetPager.LastPageText = "尾页";
        //    aspNetPager.NextPageText = "下一页";
        //    aspNetPager.PrevPageText = "上一页";
        //    aspNetPager.CustomInfoHTML = "共%PageCount%页 当前<font color=\"red\">%CurrentPageIndex%</font>/%PageCount%页 第%StartRecordIndex%-%EndRecordIndex%条";
        //    aspNetPager.TextBeforePageIndexBox = "跳转";
        //    aspNetPager.ShowCustomInfoSection = ShowCustomInfoSection.Left;
        //    //aspNetPager.ShowPageIndexBox = ShowPageIndexBox.Always;

        //    aspNetPager.SubmitButtonClass = "pageIndexButton";
        //    aspNetPager.PageIndexBoxClass = "pageIndexBox";
        //    aspNetPager.CustomInfoClass = "customHtml";
        //    aspNetPager.CssClass = "aspNetPager clearfix";
        //    aspNetPager.PagingButtonsClass = "pagingButtons";

        //    aspNetPager.UrlRewritePattern = AspNetPagerUrlRewritePattern;

        //    if (!string.IsNullOrEmpty(aspNetPager.UrlRewritePattern))
        //    {
        //        aspNetPager.EnableUrlRewriting = true;
        //    }
        //}

        #endregion
    }
}
